package com.hsy.servlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsy.service.UserService;
import com.hsy.service.impl.UserServiceImpl;
import com.hsy.pojo.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/searchUsersServlet")
public class SearchUsersServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String nickName = request.getParameter("loginName");
        String status = request.getParameter("userStatus");

        // 设置字符编码
        response.setContentType("application/json;charset=UTF-8");
        ObjectMapper objectMapper = new ObjectMapper();

        List<User> users = userService.selectUserListByNameAndStatus(nickName, status);
        String responseJson = objectMapper.writeValueAsString(users);

        // 获取响应输出流并写入 JSON 数据
        PrintWriter writer = response.getWriter();
        writer.write(responseJson);
        writer.flush();
        writer.close();

    }
}

